package com.imut.lagain.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.MoodRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 心情记录数据访问层
 */
@Mapper
public interface MoodRecordRepository extends BaseMapper<MoodRecord> {
    
    /**
     * 根据用户ID查询心情记录
     * @param userId 用户ID
     * @param page 分页参数
     * @return 心情记录分页列表
     */
    @Select("SELECT * FROM mood_record WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY record_time DESC")
    IPage<MoodRecord> findByUserId(@Param("userId") Long userId, Page<MoodRecord> page);
    
    /**
     * 根据用户ID和心情类型查询心情记录
     * @param userId 用户ID
     * @param moodType 心情类型
     * @param page 分页参数
     * @return 心情记录分页列表
     */
    @Select("SELECT * FROM mood_record WHERE user_id = #{userId} AND mood_type = #{moodType} AND is_deleted = 0 ORDER BY record_time DESC")
    IPage<MoodRecord> findByUserIdAndMoodType(@Param("userId") Long userId, @Param("moodType") String moodType, Page<MoodRecord> page);
    
    /**
     * 查询指定时间范围内的心情记录
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 分页参数
     * @return 心情记录分页列表
     */
    @Select("SELECT * FROM mood_record WHERE user_id = #{userId} AND record_time BETWEEN #{startTime} AND #{endTime} AND is_deleted = 0 ORDER BY record_time DESC")
    IPage<MoodRecord> findByUserIdAndTimeRange(@Param("userId") Long userId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, Page<MoodRecord> page);
    
    /**
     * 查询用户最近的心情记录
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 心情记录列表
     */
    @Select("SELECT * FROM mood_record WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY record_time DESC LIMIT #{limit}")
    List<MoodRecord> findRecentByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);
    
    /**
     * 统计用户心情记录数量
     * @param userId 用户ID
     * @return 心情记录数量
     */
    @Select("SELECT COUNT(*) FROM mood_record WHERE user_id = #{userId} AND is_deleted = 0")
    Long countByUserId(@Param("userId") Long userId);
    
    /**
     * 查询用户今日心情记录
     * @param userId 用户ID
     * @param startOfDay 今日开始时间
     * @param endOfDay 今日结束时间
     * @return 心情记录列表
     */
    @Select("SELECT * FROM mood_record WHERE user_id = #{userId} AND record_time BETWEEN #{startOfDay} AND #{endOfDay} AND is_deleted = 0 ORDER BY record_time DESC")
    List<MoodRecord> findTodayByUserId(@Param("userId") Long userId, @Param("startOfDay") LocalDateTime startOfDay, @Param("endOfDay") LocalDateTime endOfDay);
    
    /**
     * 查询与伴侣分享的心情记录
     * @param userId 用户ID
     * @param page 分页参数
     * @return 心情记录分页列表
     */
    @Select("SELECT * FROM mood_record WHERE user_id = #{userId} AND shared_with_partner = 1 AND is_deleted = 0 ORDER BY record_time DESC")
    IPage<MoodRecord> findSharedByUserId(@Param("userId") Long userId, Page<MoodRecord> page);
}